package com.hotacorp.opencloud.userservice.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.hotacorp.opencloud.common.commdata.Result;
import com.hotacorp.opencloud.common.log.annotation.LogAnnotation;
import com.hotacorp.opencloud.common.resourcecommon.validator.group.AddGroup;
import com.hotacorp.opencloud.common.resourcecommon.validator.group.UpdateGroup;
import com.hotacorp.opencloud.userservice.entity.SysDepartmentEntity;
import com.hotacorp.opencloud.userservice.service.SysDepartmentService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 部门信息表
 *
 * @author lwg
 * @email lwg@gmail.com
 * @date 2019-12-25 11:52:19
 */
@RestController
@Api(tags = "部门管理API")
public class SysDepartmentController extends AbstractController {
    @Autowired
    private SysDepartmentService sysDepartmentService;

    /**
     * 列表
     */
    @RequestMapping(value = "/sysdepartment/list", method = {RequestMethod.OPTIONS, RequestMethod.GET, RequestMethod.POST})
    @PreAuthorize("hasAuthority('userservice:sysdepartment:list')")
	@ApiOperation(value = "查询部门")
    @LogAnnotation(module = "部门")
    public Result<?> list(){
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put("tenantid", getTenantid());
        List<SysDepartmentEntity> page = sysDepartmentService.list(params);

        return Result.succeed("succeed", page);
    }

	/**
	 * 选择部门(添加、修改部门)
	 */
	@GetMapping("/sysdepartment/select")
	@PreAuthorize("hasAuthority('userservice:sysdepartment:select')")
	@ApiOperation(value = "添加、修改部门时选择部门")
    @LogAnnotation(module = "部门")
	public Result<?> select(){
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put("tenantid", getTenantid());
        List<SysDepartmentEntity> menuList = sysDepartmentService.list(params);
		
		//添加顶级菜单
        SysDepartmentEntity root = new SysDepartmentEntity();
		root.setDepid(0L);
		root.setDepname("总部门");
		root.setParentid(-1L);
		menuList.add(root);
		
		return Result.succeed("succeed", menuList);
	}

	/**
	 * 选择部门(添加、修改用户)
	 */
	@GetMapping("/sysdepartment/select1")
	@PreAuthorize("hasAuthority('userservice:sysdepartment:select')")
	@ApiOperation(value = "现在添加、修改用户时选择部门")
    @LogAnnotation(module = "部门")
	public Result<?> select1(){
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put("tenantid", getTenantid());
        List<SysDepartmentEntity> menuList = sysDepartmentService.list(params);
		return Result.succeed("succeed", menuList);
	}

    /**
     * 信息
     */
    @RequestMapping(value = "/sysdepartment/info/{depid}", method = {RequestMethod.OPTIONS, RequestMethod.GET, RequestMethod.POST})
    @PreAuthorize("hasAuthority('userservice:sysdepartment:info')")
	@ApiOperation(value = "部门详情")
    @LogAnnotation(module = "部门")
    public Result<?> info(@PathVariable("depid") Long depid){
		SysDepartmentEntity sysDepartment = sysDepartmentService.getById(depid);

        return Result.succeed("succeed", sysDepartment);
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/sysdepartment/save", method = {RequestMethod.OPTIONS, RequestMethod.POST})
    @PreAuthorize("hasAuthority('userservice:sysdepartment:save')")
	@ApiOperation(value = "添加部门")
    @LogAnnotation(module = "部门")
    public Result<?> save(@RequestBody @Validated(AddGroup.class)  SysDepartmentEntity sysDepartment){
    	sysDepartment.setTenantid(getTenantid());
		sysDepartmentService.save(sysDepartment);

        return Result.succeed();
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/sysdepartment/update", method = {RequestMethod.OPTIONS, RequestMethod.POST})
    @PreAuthorize("hasAuthority('userservice:sysdepartment:update')")
	@ApiOperation(value = "修改部门")
    @LogAnnotation(module = "部门")
    public Result<?> update(@RequestBody @Validated(UpdateGroup.class) SysDepartmentEntity sysDepartment){
		int ret = sysDepartmentService.updateById(sysDepartment, getTenantid());
		if (ret == 0) {
	        return Result.succeed();
		} else if (ret == 1) {
			return Result.failed("修改不是本租户的部门，修改失败！");
		} else if (ret == 2) {
			return Result.failed("把上级部门改为现有上级部门的子部门是不允许的，修改失败！");
		} else if (ret == 3) {
			return Result.failed("更新数据失败！");
		} else {
			return Result.failed("更新数据失败！");
		}
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/sysdepartment/delete", method = {RequestMethod.OPTIONS, RequestMethod.POST})
    @PreAuthorize("hasAuthority('userservice:sysdepartment:delete')")
	@ApiOperation(value = "删除部门")
    @LogAnnotation(module = "部门")
    public Result<?> delete(@RequestBody Long[] depids){
		sysDepartmentService.removeByIds(Arrays.asList(depids), getTenantid());

        return Result.succeed();
    }

}
